
package com.htd.htdapmservice.xxljob;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.htd.htdapmapi.alarm.request.AlarmInto;
import com.htd.htdapmapi.alarm.result.AlarmResult;
import com.htd.htdapmcommon.constants.ErrorCodeConstants;
import com.htd.htdapmcommon.enums.AlarmSendStatus;
import com.htd.htdapmcommon.enums.AlarmsLevelEnum;
import com.htd.htdapmcommon.enums.AlarmsStatusEnum;
import com.htd.htdapmdmo.AlarmsInfo;
import com.htd.htdapmservice.service.intf.AlarmsInfoService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;

/**
 * 告警信息task
 */
@JobHandler(value="alarmsInfoJobHandler")
@Component
public class AlarmsInfoJobHandler extends IJobHandler {
    private static final Logger logger = LoggerFactory.getLogger(AlarmsInfoJobHandler.class);

    @Autowired
    private AlarmsInfoService alarmsInfoService;
 
	@Override
	public ReturnT<String> execute(String param) throws Exception {

        logger.info("alarmsInfoJobHandler Entrance, param : {}", new Object[]{JSONObject.toJSONString(param)});
        List<AlarmsInfo> alarmsInfoList = new ArrayList<AlarmsInfo>();
        try{
            
            //获取当前小时内的错误日志
            alarmsInfoList = alarmsInfoService.getAlarmsInfoListBySendStatus(AlarmSendStatus.SEND_NO.getCode());
            
            if (alarmsInfoList!=null &&alarmsInfoList.size()>0) {
                AlarmInto alarmInto  =null;
                for (AlarmsInfo alarmsInfo:alarmsInfoList) {
                    alarmInto= new AlarmInto();
                    alarmInto.setEventType(AlarmsStatusEnum.ALARM_TRIGGER.getCode());
                    alarmInto.setPriority(AlarmsLevelEnum.LEVEL_TWO.getCode());
                    alarmInto.setProductType(alarmsInfo.getAlarmsProductTypeCode());
                    alarmInto.setAlarmsDetail(alarmsInfo.getAlarmsDetail());
                    alarmInto.setAlarmsTitle(alarmsInfo.getAlarmsTitle());
                    AlarmResult alarmResult = alarmsInfoService.createAlarm(alarmInto);
                    if(ErrorCodeConstants.SUCCESS_CODE.equals(alarmResult.getResponseCode())){
                        Map<String,Object> map = new HashMap<String,Object>();
                        map.put("sendStatus",AlarmSendStatus.SEND_YES);
                        map.put("alarmsId",alarmsInfo.getAlarmsId());
                        map.put("updateTime",new Date());
                        alarmsInfoService.updateSendStatusByAlarmsId(map);
                    }
                }
            }
            
        } catch (Exception e){
            logger.error("alarmsInfoJobHandler error:", e);
            return FAIL;
        }
		
		return SUCCESS;
	}

}


